package com.fmsz.xmly.fragments.findfgts;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.test.mock.MockApplication;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.classify.FindClassifyAdapter;
import com.fmsz.xmly.application.MyApplication;
import com.fmsz.xmly.callbacks.FindClassifyHttpService;
import com.fmsz.xmly.entitys.classify.FindClassifyEntity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class FindClassifyFragment extends Fragment implements Callback<FindClassifyEntity> {


    private View view;
    private RecyclerView recyclerView;
    private FindClassifyAdapter adapter;
    private final String BASE_URL = "http://mobile.ximalaya.com/";
    private static final String TAG = "FindClassifyFragment";

    public FindClassifyFragment() {
        // Required empty public constructor
    }


    //加载数据
    private void loadData() {
        MyApplication application = (MyApplication) getActivity().getApplication();
        application.getClassifyService().getDatas(13).enqueue(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view==null) {
            view = inflater.inflate(R.layout.fragment_find__classify_, container, false);
            initView(view);
            loadData();
        }
        return view;
    }

    //初始化UI
    private void initView(View view) {
        recyclerView = ((RecyclerView) view.findViewById(R.id.recycle_find_classify));
        GridLayoutManager manager = new GridLayoutManager(getActivity(),2);
        recyclerView.setLayoutManager(manager);
        adapter = new FindClassifyAdapter(getContext());
        View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.find_classify_head,recyclerView,false);
        recyclerView.setAdapter(adapter);
        adapter.setHeadView(headerView);
        
    }


    //retrofit数据下载回调接口
    @Override
    public void onResponse(Call<FindClassifyEntity> call, Response<FindClassifyEntity> response) {
        if (response.body().getList()!=null && adapter!=null) {
            adapter.addAll(response.body().getList());
            setFootView();
        }
    }

    private void setFootView() {
        View footView = LayoutInflater.from(getContext()).inflate(R.layout.find_classify_foot,recyclerView,false);
        adapter.setFootView(footView);
    }

    @Override
    public void onFailure(Call<FindClassifyEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
